package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.CompanyMapper;
import com.pojo.TbCompany;
import com.pojo.TbProducts;
import com.pojo.TbWorker;
import com.service.CompanyService;
import com.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, TbCompany> implements CompanyService {
    @Autowired
    JSONResult jsonResult;

    @Override
    public JSONResult findCompanyById(int companyId) {
        QueryWrapper<TbCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        TbCompany company = this.getOne(queryWrapper);
        if (company!=null){
            return jsonResult.success(200,"查询成功",company,null);
        }
        return jsonResult.failure(201,"查询失败",null,null);
    }

    @Override
    public JSONResult updateCompany(TbCompany company) {
        boolean b = this.saveOrUpdate(company);
        if (b){
            return jsonResult.success(200,"修改成功",null,null);
        }
        return jsonResult.failure(201,"修改失败",null,null);
    }

    @Override
    public JSONResult deleteCompany(int companyId) {
        boolean b = this.removeById(companyId);
        if (b){
            return jsonResult.success(200,"删除成功",null,null);
        }
        return jsonResult.failure(201,"删除失败",null,null);
    }

    @Override
    public JSONResult addCompany(TbCompany tbCompany) {
        boolean save = this.save(tbCompany);
        if (save){
            return jsonResult.success(200,"添加成功",null,1L);
        }
        return jsonResult.failure(201,"添加失败",null,0L);
    }

    @Override
    public JSONResult findAllCompanyByState(int page, int limit, int state) {
        Page<TbCompany> tbpage = new Page<>(page,limit);
        if (state==0&&state!=-1){
            QueryWrapper<TbCompany> tbCompany = new QueryWrapper<>();
            tbCompany.eq("company_state",state);
            Page<TbCompany> page1 = this.page(tbpage,tbCompany);
            return jsonResult.success(200,"待审核查询成功",page1,page1.getTotal());
        }else if (state==1&&state!=-1){
            QueryWrapper<TbCompany> tbCompany = new QueryWrapper<>();
            tbCompany.eq("company_state",state);
            Page<TbCompany> page1 = this.page(tbpage,tbCompany);
            return jsonResult.success(200,"已通过查询成功",page1,page1.getTotal());
        }else if (state==2&&state!=-1){
            QueryWrapper<TbCompany> tbCompany = new QueryWrapper<>();
            tbCompany.eq("company_state",state);
            Page<TbCompany> page1 = this.page(tbpage,tbCompany);
            return jsonResult.success(200,"已驳回查询成功",page1,page1.getTotal());
        }
        return jsonResult.failure(201,"状态码错误,查询失败",null,null);
    }

    @Override
    public JSONResult findAllCompany(int page, int limit) {
        Page<TbCompany> tbpage = new Page<>(page,limit);
        QueryWrapper<TbCompany> tbCompany = new QueryWrapper<>();
        Page<TbCompany> page1 = this.page(tbpage,tbCompany);
        return jsonResult.success(200,"查询成功",page1,page1.getTotal());
    }

    @Override
    public JSONResult findAllCompanyByCompanyName(int page, int limit, String companyName) {
        Page<TbCompany> tbpage = new Page<>(page,limit);
        if (companyName!=null&&companyName!="-1"){
            QueryWrapper<TbCompany> tbCompany = new QueryWrapper<>();
            tbCompany.eq("company_name",companyName);
            Page<TbCompany> page1 = this.page(tbpage,tbCompany);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        return jsonResult.success(201,"查询失败",null,null);
    }
}
